# coding = utf-8

'''
针对3d ircadb的相关肿瘤统计
'''

import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
from scipy import ndimage
from skimage import measure
import prettytable as pt

import SimpleITK as sitk

#获取肿瘤数量的真实大小
def get_tumor_count_v2():
    data_path = "E:\Dataset\Liver\\3Dircadb"
    count_list = []
    for i in range(20):
        case_id = "case_{}".format(str(i).zfill(5))
        case_path = os.path.join(data_path, case_id)
        case_path = os.path.join(case_path, "segmentation")
        np_data = []
        for item in sorted(os.listdir(case_path)):
            item_file = os.path.join(case_path, item)
            data = np.load(item_file)
            np_data.append(data)
        np_data = np.array(np_data)
        np_data[np_data == 1] = 0
        np_data[np_data == 2] = 1

        print("*"*35, case_id, "*"*35)

        if np.max(np_data) == 0:
            print()
            continue

        [tumor_labels, num] = measure.label(np_data, return_num=True)
        print("tumor size:", np_data.sum())
        print("image shape:", np_data.shape)
        print("tumor count:", num)
        region = measure.regionprops(tumor_labels)

        tb = pt.PrettyTable()
        tb.field_names = ["tumor NO.", "tumor size", "slice begin", "slice end", "total slice", "avg per slice"]
        for i in range(num):
            tb.add_row([
                i, region[i].area, region[i].bbox[0], region[i].bbox[3], region[i].bbox[3] - region[i].bbox[0],
                int(region[i].area / (region[i].bbox[3] - region[i].bbox[0]))]
            )
            count_list.append(int(region[i].area / (region[i].bbox[3] - region[i].bbox[0])))
        print(tb)
        print()

    print(len(count_list))
    print(count_list)


def analysis_tumor_size():
    tumor = [4387, 850, 653, 323, 1079, 153, 474, 639, 791, 11, 51, 117, 106, 237, 151, 119, 10, 4578, 178, 118, 637, 452, 1185, 628, 89, 159, 880, 120, 226, 121, 273, 159, 382, 769, 188, 1316, 1, 255, 104, 230, 48, 369, 424, 28, 68, 4591, 425, 240, 113, 339, 160, 91, 830, 165, 1368, 1191, 88, 213, 138, 123, 452, 362, 117, 221, 28, 489, 3179, 3232, 412, 161, 54, 762, 104, 178, 134, 252, 144, 348, 189, 1210, 36, 110, 178, 93, 374, 56, 170, 261, 42, 26, 128, 62, 113, 26, 59, 84, 27, 33, 237, 67, 40, 75, 149, 245, 235, 71, 414, 89, 48, 120, 627, 127, 118, 209, 27, 62, 38]
    tumor_0_10 = 0
    tumor_10_20 = 0
    tumor_20_50 = 0
    tumor_50_100 = 0
    tumor_100_500 = 0
    tumor_500= 0

    for item in tumor:
        if item <= 10:
            tumor_0_10 += 1
        elif item <= 20:
            tumor_10_20 += 1
        elif item <= 50:
            tumor_20_50 += 1
        elif item <= 100:
            tumor_50_100 += 1
        elif item <= 500:
            tumor_100_500 += 1
        else:
            tumor_500 += 1

    tb = pt.PrettyTable()
    tb.field_names = ["0-10", "10-20", "20-50", "50-100", "100-500", "500+"]
    tb.add_row([tumor_0_10, tumor_10_20, tumor_20_50, tumor_50_100, tumor_100_500, tumor_500])
    tb.add_row([tumor_0_10/len(tumor), tumor_10_20/len(tumor), tumor_20_50/len(tumor), tumor_50_100/len(tumor),
                tumor_100_500/len(tumor), tumor_500/len(tumor)])
    print(tb)


if __name__ == '__main__':
    analysis_tumor_size()

